package com.nmu.app.Shrimp.veiw.javagame.render;

import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import com.nmu.app.Shrimp.veiw.javagame.util.KeyboardInput;
import com.nmu.app.Shrimp.veiw.javagame.util.SimpleKeyboardInput;

public class KeyboardExample extends JFrame implements Runnable{
	private volatile boolean running;
	private Thread gameThread;
	private KeyboardInput keys;
	private boolean space;
	
	public KeyboardExample() {
		keys=new KeyboardInput();
	}
	protected void createAndShowGUI(){
		setTitle("key board input");
		setSize(320,240);
		addKeyListener(keys);
		setVisible(true);
		gameThread=new Thread(this);
		gameThread.start();
	}
	public void run() {
		running=true;
		while(running){
			gameLoop();
		}
	}
	public void gameLoop(){
		keys.poll();
		if(keys.keyDown(KeyEvent.VK_SPACE))System.out.println("VK_SPACE");
		if(keys.keyDown(KeyEvent.VK_UP))System.out.println("VK_UP");
		if(keys.keyDown(KeyEvent.VK_DOWN))System.out.println("VK_DOWN");
		if(keys.keyDown(KeyEvent.VK_LEFT))System.out.println("VK_LEFT");
		if(keys.keyDown(KeyEvent.VK_RIGHT))System.out.println("VK_RIGHT");
		try{
			Thread.sleep(10);
		}catch(InterruptedException e){System.err.println("errInterupt");}
	}
	
	protected void onWindowClosing(){
		try{
			running=false;
			gameThread.join();
			System.out.println("stopping...");
		}catch(InterruptedException e){
			e.printStackTrace();
		}
		System.exit(0);
	}
	
	public static void main(String[] args) {
		final KeyboardExample app=new KeyboardExample();
		app.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				app.onWindowClosing();
			}
		});
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {	
				app.createAndShowGUI();
			}
		});
		
	}
	
	

}
